package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Preject;
import com.itheima.mapper.PrejectMapper;
import com.itheima.pojo.jo.ExcelStatisticsJo;
import com.itheima.pojo.jo.ProjectStatisticsJo;
import com.itheima.pojo.po.ProjectStatisticsPo;
import com.itheima.query.ProjectPageQuery;
import com.itheima.result.PageResult;
import com.itheima.service.PrejectService;
import com.itheima.utils.Result;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private PrejectMapper prejectMapper;
    @Autowired
    private PrejectService prejectService;

    @GetMapping("/all")
    private Result all(){
        List<Preject> prejects = new ArrayList<>();
        prejects = prejectMapper.setlectall();
        return Result.success(prejects);
    }
    //获取指导教师列表
    @GetMapping("/teacherType")
    public Result teacherType(){
        List<Map<String,Object>> list = prejectMapper.selectTeacherType();
        return Result.success(list);
    }
    // 根据id查询项目信息
    @GetMapping("/selectById")
    public Result selectById(@RequestParam(value = "id") Integer id){
        if (id==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        Preject preject = prejectMapper.selectProjectById(id);
        if (preject==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success(preject);
    }
    // 根据id查询项目信息
    @GetMapping("/selectByIds")
    public Result selectByIds(@RequestParam(value = "ids") Integer[] ids){
        if (ids.length<1){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        List<Preject> prejects = prejectMapper.selectProjectByIds(ids);
        return Result.success(prejects);
    }


    @GetMapping("/title")
    public Result selectTitle(@RequestParam(value = "title") String title){
        if (title.equals("")){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        System.out.println(title);
        Preject preject = new Preject();
        preject = prejectMapper.selsectTitle(title);
        if (preject!=null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success();
    }
    @PostMapping("/projectAdd")
    public Result projectAdd(@RequestBody Preject preject){
        if (preject.getTitle()==null || preject.getContent()==null ||
                preject.getProjectImg()==null || preject.getDownload()==null ||
                preject.getType()==null || preject.getMenber()==null ||
                preject.getUsername()==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        prejectService.projectAdd(preject);
        return Result.success();
    }
    //批量新增
    @PostMapping("/projectBatchAdd")
    @ApiOperation("批量新增")
    public Result projectBatchAdd(@RequestBody List<Preject> prejects){
        if (prejects.size()<1){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        prejectService.projectBatchAdd(prejects);
        return Result.success();
    }

    @PostMapping("/projectEdit")
    public Result projectEdit(@RequestBody Preject preject){
        if (preject.getTitle()==null || preject.getContent()==null ||
                preject.getProjectImg()==null || preject.getDownload()==null ||
                preject.getType()==null || preject.getMenber()==null ||
                preject.getUsername()==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        prejectService.projectEdit(preject);
        return Result.success();
    }
    @PostMapping("/projectPage")
    public Result<PageResult> projectPage(@RequestBody ProjectPageQuery projectPageQuery){
        if (projectPageQuery.getPageSize()==null || projectPageQuery.getPageNum()==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        PageResult pageResult = prejectService.pageQuery(projectPageQuery);
        return Result.success(pageResult);

    }
    @PostMapping("/projectDelects")
    public Result projectDelects(@RequestBody String Ids){
        String string = Ids.substring(8,Ids.length()-2);
        if (string.length()<1){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        prejectService.Delects(string);
        return Result.success();
    }
    @PostMapping("/projectStatistics")
    @ApiOperation("数据统计")
    public Result<ProjectStatisticsPo> projectStatistics(@RequestBody ProjectStatisticsJo projectJo){
        ProjectStatisticsPo projectPo = prejectService.getProjectStatistics(projectJo);
        return Result.success(projectPo);
    }
    /**
     * 导出运营数据报表
     * @param projectJo
     */
    @PostMapping("/export")
    @ApiOperation("导出运营数据报表")
    public void export(@RequestBody ExcelStatisticsJo projectJo, HttpServletResponse response) throws IOException {
        prejectService.exprotBusinsData(projectJo,response);
    }





}
